<?php

namespace App\Http\Controllers\Admin;

use App\Handlers\ImageUploadHandler;
use App\Http\Requests\NewRequest;
use App\Models\News;
use App\Models\NewsSort;
use Illuminate\Http\Request;


class NewsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        return News::where(function($query) use ($request){
            // 关键字
            if ($request->key){
                $key = '%'. $request->key .'%';
                $query->where('title', 'like', $key)->orWhere('content', 'like', $key)->orWhere('author', 'like', $key);
            }
        })->where(function($query) use ($request){
            // 日期
            if ($request->date){
                $date = explode(' - ', $request->date);
                if (!isset($date[1])){
                    return;
                }

                $query->whereBetween('created_at', [$date[0], $date[1]]);
            }
        })->with('newsSort')->latest()->paginate();
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $newsSorts = NewsSort::orderBy('sort')->get(['id', 'sort_name']);
        $bridges = [];
        return compact('newsSorts', 'bridges');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 上传文件
        if ($request->file('news')){
            return $this->upload($request->file('news'));
        }

        $this->_validator($request);
        $data = $request->only('sort_id', 'bridge_id', 'title', 'author', 'image', 'content');
        return News::create($data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(News $news)
    {
        $newsSorts = NewsSort::orderBy('sort')->get(['id', 'sort_name']);
        $bridges = [];
        return compact('newsSorts', 'bridges', 'news');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(News $news)
    {
        $newsSorts = NewsSort::orderBy('sort')->get(['id', 'sort_name']);
        $bridges = [];
        return compact('newsSorts', 'bridges', 'news');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, News $news)
    {
        // 上传文件
        if ($request->file('news')){
            return $this->upload($request->file('news'));
        }

        $this->_validator($request);
        $data = $request->only('sort_id', 'bridge_id', 'title', 'author', 'image', 'content');
        $news->update($data);
        return $news;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(News $news)
    {
        $news->delete();
    }

    /**
     * 数据验证
     *
     * @param Request $request
     */
    protected function _validator(Request $request)
    {
        $this->validate($request, [
            'sort_id' => 'exists:news_sorts,id',
            'title' => 'required',
            'author' => 'required',
            'content' => 'required',
        ], [

        ]);
    }
}
